package com.example.sinoyd.frameapplication.ProjectCodes.Fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.gson
import com.example.sinoyd.frameapplication.KotlinFrame.Frame.Uitl.Networkrequestmodel
import com.example.sinoyd.frameapplication.KotlinFrame.Mode.Measuringpoints
import com.example.sinoyd.frameapplication.KotlinFrame.Uitl.BaiduMapControl
import com.example.sinoyd.frameapplication.KotlinFrame.Uitl.showdialog
import com.example.sinoyd.frameapplication.ProjectCodes.Adapter.GisAdapter
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetAllAQI
import com.example.sinoyd.frameapplication.R
import com.sinoyd.Code.Until.Networkrequestaddress
import com.sinoyd.Code.Until.SharedPreferencesFactory
import kotlinx.android.synthetic.main.gis_fragment.*
import kotlinx.android.synthetic.main.include_title_layout.*
import okhttp3.Response
import org.jetbrains.anko.act
import org.jetbrains.anko.toast

/**
 * GIS监控界面
 * **/
@SuppressLint("ValidFragment")
class GisFragment(var name: String = "GIS监控") : BaseFragment() {
    var baiducontrol: BaiduMapControl? = null
    var n = 0
    var adapter: GisAdapter? = null

    private val handler = object : Handler() {

        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                1 -> {
                    iv_gis.setImageResource(R.drawable.jiantoushang)
                    ll_show.visibility = View.VISIBLE
                }
                2 -> {
                    iv_gis.setImageResource(R.drawable.jiantouxia)
                    ll_show.visibility = View.GONE
                }
            }

        }
    }

    //选择站点后进行的操作
    override fun selectPortCallBack() {
        toast("选择站点后一系列请求")
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val conview = inflater.inflate(R.layout.gis_fragment, null)
        return conview
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        baiducontrol = BaiduMapControl(act, bmapView)
        //定位自己
        baiducontrol!!.requestLocation()
        setview()
        setlisterens()
        getAllAQI()
    }


    //设置界面
    private fun setview() {
        iv_left_two.visibility = View.INVISIBLE
        iv_left_one.visibility = View.INVISIBLE
        center_title.text = name
        when (SharedPreferencesFactory.getdata(activity, "OS")) {
            "air" -> {
                gis_tv_aqi.visibility = View.VISIBLE
            }
            "water" -> {
                gis_tv_aqi.visibility = View.GONE
            }
        }

        //移动地图
        lv_gis.setOnItemClickListener { adapterView, view, i, l ->
            baiducontrol!!.moveBaidumap(getAllAQI.aqiInfo[i].y.toDouble(), getAllAQI.aqiInfo[i].x.toDouble())
        }

    }

    //设置各种监听
    private fun setlisterens() {

        iv_gis.setOnClickListener {
            n++
            val message = handler.obtainMessage()
            if (n % 2 == 1) {
                message.what = 1
                handler.sendMessage(message)
            } else {
                message.what = 2
                handler.sendMessage(message)
            }
        }
    }

    //获取GIS上所有信息
    fun getAllAQI() {
        showdialog(activity, "loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("getAllAQI")
                .seturl(Networkrequestaddress.URL_GetAllAQI)
                .addparam("sysType", SharedPreferencesFactory.getdata(activity, "OS"))
                .start(this)
    }

    var getAllAQI = GetAllAQI()
    var listmapdata: ArrayList<Measuringpoints> = ArrayList()
    override fun requestSuccess(response: Response, TAG: String) {
        super.requestSuccess(response, TAG)
        when (TAG) {
            "getAllAQI" -> {
                getAllAQI = gson.fromJson(responsestr, GetAllAQI::class.java)
                if (getAllAQI.aqiInfo == null || getAllAQI.aqiInfo.size == 0) {
                } else {
                    for (item in getAllAQI.aqiInfo) {
                        listmapdata.add(Measuringpoints(item.pointName, item.y.toDouble(), item.x.toDouble(), item.aqi, item.grade))
                    }
                    //显示地图
                    baiducontrol!!.initOverlay(listmapdata)
                    //显示listview
                    adapter = GisAdapter(getAllAQI.aqiInfo as ArrayList<GetAllAQI.AQIInfoBean>, activity)
                    lv_gis.adapter = adapter
                }
                //平移到闵行
                baiducontrol!!.moveBaidumap(31.115627, 121.387466)
            }

        }
    }
}